#include "commanpage.h"
#include "ui_commanpage.h"

CommanPage::CommanPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommanPage)
{
    ui->setupUi(this);
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    connect(ui->pageMusicList,&QListWidget::doubleClicked,this,[=](const QModelIndex& index){
        emit this->playMusicByIndex(this,index.row());
        //qDebug()<<"emited this->playMusicByIndex(this,index.row());";
    });

}

CommanPage::~CommanPage()
{
    delete ui;
}

void CommanPage::setMusicPageType(MusicPageType musicPageType)
{
    this->musicPageType=musicPageType;
}

void CommanPage::setCommanPageUi(const QString &titleText, const QString &coverPath)
{
    ui->pageTitle->setText(titleText);

    ui->musicPlayImage->setPixmap(QPixmap(coverPath));
    ui->musicPlayImage->setScaledContents(true);

//    ListItem *listitem=new ListItem(this);
//    QListWidgetItem *item=new QListWidgetItem(ui->pageMusicList);
//    item->setSizeHint(QSize(listitem->width(),listitem->height()));
//    ui->pageMusicList->setItemWidget(item,listitem);
}

void CommanPage::addMusicToMusicPage(MusicList *musicList)
{
    if(musicList==nullptr)
    {
        qDebug()<<"musicList==nullptr";
        return;
    }
   // qDebug()<<"void CommanPage::addMusicToMusicPage(MusicList &musicList)";
    //qDebug()<<this->musicPageType;
    this->musicOfPage.clear();
    for(auto music:musicList->getMusicList())
    {
        switch (this->musicPageType)
        {
            case MusicPageType::LIKE_PAGE:
            {
                if(music.getIsLike())
                    this->musicOfPage.push_back(music.getMusicId());
                break;
            }
            case MusicPageType::LOCAL_PAGE:
            {
                this->musicOfPage.push_back(music.getMusicId());
                break;
            }
            case MusicPageType::HISTORY_PAGE:
            {
                if(music.getIsHistory())
                    this->musicOfPage.push_back(music.getMusicId());
                break;
            }
            default:
                qDebug()<<"暂未支持";
                break;
        };
    }
}

void CommanPage::addMusicToPlayList(MusicList *musicList, QMediaPlaylist *playList)
{
    for(auto music:*musicList)
    {
        switch(this->musicPageType)
        {
            case MusicPageType::LIKE_PAGE:
            {
                if(music.getIsLike())
                    playList->addMedia(music.getMusicUrl());
                break;
            }
            case MusicPageType::LOCAL_PAGE:
            {
                playList->addMedia(music.getMusicUrl());
                break;
            }
            case MusicPageType::HISTORY_PAGE:
            {
                if(music.getIsHistory())
                    playList->addMedia(music.getMusicUrl());
                break;
            }
            default:
                qDebug()<<"未知页";
                break;

        }
    }
}

void CommanPage::reFresh(MusicList *musicList)
{
    if(musicList==nullptr)
    {
        qDebug()<<"musicList==nullptr";
        return;
    }
    //qDebug()<<"void CommanPage::reFresh(MusicList musicList)";
    ui->pageMusicList->clear();
     this->addMusicToMusicPage(musicList);

    for(auto musicId:this->musicOfPage)
    {
        auto it=musicList->findMusicByMusicId(musicId);
        if(it==musicList->end())
              continue;
        ListItem *listitem=new ListItem(this);
        listitem->setMusicNameLabel(it->getMusicName());
        listitem->setSingerNameLabel(it->getMusicSinger());
        listitem->setCoverNameLabel(it->getMusicCover());
        listitem->setIsLike(it->getIsLike());
        listitem->setMusicId(musicId);
        QListWidgetItem *item=new QListWidgetItem(ui->pageMusicList);
        item->setSizeHint(QSize(listitem->width(),listitem->height()));
        ui->pageMusicList->setItemWidget(item,listitem);
        //qDebug()<<"connect isLikeClick";
        connect(listitem,&ListItem::isLikeClick,this,[=](bool isLike){
            //qDebug()<<"process isLikeClick";
            if(it==nullptr)
                  qDebug()<<"it==nullptr";
           emit updateLikeMusic(isLike,it->getMusicId());
            //qDebug()<<"emited updateLikeMusic(isLike,it->getMusicId());";
        });

        connect(listitem,&ListItem::moreBtClick,this,[=](QMenu*menu,int width,int height){
            emit moreSet(menu,width,height);
            qDebug()<<" emit moreShow(topRight);";
        });

        connect(listitem,&ListItem::itemPlayClick,this,[=](const QString&musicId){
           emit moreItemPlayClick(musicId);
           qDebug()<<"emit moreItemPlayClick();";
        });
        connect(listitem,&ListItem::itemNextClick,this,[=](const QString&musicId){
           emit moreItemNextClick(musicId);
        });
        connect(listitem,&ListItem::itemDeleteClick,this,[=](const QString&musicId){
           emit moreItemDeleteClick(musicId);
        });
        connect(listitem,&ListItem::itemCommendClick,this,[=](const QString&musicId){
           emit moreItemCommendClick(musicId);
        });

    }
    repaint();
}

QString CommanPage::getMusicIdByIndex(int index)
{
    if(index>=this->musicOfPage.size()||index<0)
        qDebug()<<"无此歌曲";
    return this->musicOfPage[index];
}

void CommanPage::on_playAll_clicked()
{
    emit playAllClick(this->musicPageType);
}
